package jed.DistributedEventingTests;

import javaEventing.EventManager;
import jed.DistributedEventManagerExtension;
import jed.DistributedEventObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Espen Skjervold, FFI, 01.feb.2011
 */
public class ChatAppExample2 {


    public static void main(String[] args) {

        try {

            System.out.println("Your name: ");
            final String myName = (new BufferedReader(new InputStreamReader(System.in)).readLine());

            EventManager.Condition condition = new EventManager.Condition() {
                public boolean matches(Object o) {
                    return o.toString().contains(myName+":");
                }
            };


            EventManager.setEventManagerExtension(new DistributedEventManagerExtension());

            EventManager.registerEventListener(new EventManager.GenericEventListener() {
                 public void eventTriggered(Object sender, EventManager.Event event) {
                     System.out.println("Chat message: " + event.getPayload());
                 }
             }, new DistributedEventObject(), condition);


            System.out.println("Chat now by prefixing your message with <recipient>:");
            while (true) {
                String message = (new BufferedReader(new InputStreamReader(System.in)).readLine());
                EventManager.triggerEvent(null, new DistributedEventObject(message), message);  // the message is also sent as the condition
            }


        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
